package org.adorsys.aderp.cash.web.ext;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.adorsys.aderp.cash.domain.CashReceipt;
import org.adorsys.aderp.cash.domain.CashRegItem;
import org.adorsys.aderp.cash.service.CashReceiptService;
import org.adorsys.aderp.cash.service.CashRegItemService;
import org.adorsys.aderp.cash.work.encashment.redirect.CashRegItemComponent;
import org.adorsys.aderp.cash.work.encashment.redirect.DataCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

@RequestMapping(value="/incommingclient")
@Controller
public class IncommingRequestController {
	@Autowired
	private CashRegItemService cashRegItemService;
	
	@Autowired
	private CashReceiptService cashReceiptService;
	
	
	@RequestMapping(method=RequestMethod.GET)
	public String handleIncommingRequest(@RequestParam(required=false, value="moduleCallingUrlValue")String moduleCalling,HttpServletRequest request, Model uiModel){
		HttpSession session = request.getSession();
		CashRegItemComponent cashRegItemComponent = (CashRegItemComponent) session.getAttribute("cashRegisterComponent");
//		DataCollection dataCollection = (DataCollection) session.getAttribute("dataCollection");
		DataCollection dataCollection = (DataCollection) request.getAttribute("dataCollection");
		System.out.println("\n \n Data Collection  :  "+dataCollection.toString());
		//TODO : process creation of cashregisteritem
		if(cashRegItemComponent == null) throw new NullPointerException("Null value for cash register item component");
		CashRegItem cashRegItem = new CashRegItem();
		cashRegItem.setCashRegItemKey(cashRegItemComponent.getCashRegItemKey());
		cashRegItem.setAmount(cashRegItemComponent.getAmount());
		cashRegItem.setQty(cashRegItemComponent.getQty());
		cashRegItem.setUnitPrice(cashRegItemComponent.getUnitPrice());
		cashRegItemService.saveCashRegItem(cashRegItem);
		
		// TODO : continue processing cashregister items
		CashReceipt cashReceipt = new CashReceipt();
		HashSet<CashRegItem> hashSet = new HashSet<CashRegItem>();
		hashSet.add(cashRegItem);
		cashReceipt.setCashRegItems(hashSet);
		cashReceiptService.saveCashReceipt(cashReceipt);
		 uiModel.addAttribute("cashreceipt", cashReceipt);
		return "redirect:cashreceipts/show";
	}
	
	@RequestMapping(method=RequestMethod.GET,params={"collectionKey","callbackurl"})
	public String handleBookingCall(@RequestParam("collectionKey")String collectionKey,@RequestParam("callbackurl")String callbackurl,Model uiModel,
			HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws URISyntaxException{
		System.out.println("\n first step complete \n");
		RestTemplate restTemplate = new RestTemplate();
		@SuppressWarnings("unchecked")
		List<CashRegItem> data = restTemplate.getForObject("http://localhost:8080/aderp-booking/bookingtocash/restfullcallback?collectionKey="+collectionKey, new ArrayList<CashRegItem>().getClass());
		System.out.println("\n \n Retrieved Data  : "+data.toString());
		return "redirect:/";
	}
}
